package com.pj.lib.auraServer.components.userManagement;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.pj.lib.auraServer.app.Server;
import com.pj.lib.auraServer.app.ServerComponent;
import com.pj.lib.auraServer.app.listeners.OnUserChange;
import com.pj.lib.auraServer.components.DB.DB;
import com.pj.lib.auraServer.components.DB.tables.AuraType;
import com.pj.lib.auraServer.components.DB.tables.User;
import com.pj.lib.auraServer.components.connection.ClientThread;
import com.pj.lib.auraServer.exceptions.AuraServerException;
import com.pj.lib.aura.protocol.AuraProtocol;

public class UserAuraConnectorComponent extends ServerComponent{

	public UserAuraConnectorComponent(Server server) {
		super(server);
		
		addOnUserChange(new OnUserChange() {
			
			@Override
			public void userLogout(int userId) {
				//nothing
				
			}
			
			@Override
			public void userLogin(ClientThread ct, String username, String password) {
				//nothing
				
			}
			
			@Override
			public void userCreate(ClientThread ct, String username, String password) {
				//nothing
				
			}
			
			@Override
			public void setAura(ClientThread ct, String code, int userId) {
				handleSetAura(ct, code, userId);
				
			}
			
			@Override
			public void disableAura(ClientThread ct, int userId) {
				handleDisableAura(ct, userId);
				
			}
		});
	}
	
	protected void handleDisableAura(ClientThread ct, int userId) {
		HashMap<String, Object> values = new HashMap<String, Object>();
		values.put(User.COLUMN_AURA_TYPE, "");
		try {
			getDB().getInstance(DB.INSTANCE_USER).update(values, User.COLUMN_ID + " = ?", new Object[] {userId});
			fireUserAuraDisabledEvent(userId);
			ct.handleSendDisableAura(true, null);
		} catch (AuraServerException e) {
			AuraServerException
				.handleExceptionInComponent(e, this, ct, AuraProtocol.DISABLE_AURA_FOR_USER, 
						false, null);
		} catch (SQLException e) {
			AuraServerException
				.handleExceptionInComponent(e, this, ct, AuraProtocol.DISABLE_AURA_FOR_USER, 
						false, null);
		}
	}

	
	protected void handleSetAura(ClientThread ct, String code, int userId) {
		
		
		List<Map<String, Object>> codeAuraType;
		try {
			codeAuraType = getDB().getInstance(DB.INSTANCE_AURA_TYPE)
				.get(null, " " + AuraType.COLUMN_CODE + " = ?", new String[] {code}, null);
			if(codeAuraType.size() == 1) {
				HashMap<String, Object> values = new HashMap<String, Object>();
				values.put(User.COLUMN_AURA_TYPE, code);
				
				getDB().getInstance(DB.INSTANCE_USER).update(values, User.COLUMN_ID + " = ?", new Object[] {userId});
				fireAfterUserAuraSetEvent(userId, code);
				ct.handleSendSetAura(true, null);
			} else {
				ct.handleSendSetAura(false, "lib_there_are_no_such_aura_type");
			}
		} catch (AuraServerException e) {
			AuraServerException
			.handleExceptionInComponent(e, this, ct, AuraProtocol.SET_AURA_FOR_USER, 
					false, null);
		} catch (SQLException e) {
			AuraServerException
			.handleExceptionInComponent(e, this, ct, AuraProtocol.SET_AURA_FOR_USER, 
					false, null);
		}
		
		
		
	}

}
